package com.jf3q.iot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.iot.domain.IotDevice;
import com.jf3q.iot.domain.IotGateway;
import com.jf3q.iot.mapper.IotGatewayMapper;
import com.jf3q.iot.service.IotDeviceService;
import com.jf3q.iot.mapper.IotDeviceMapper;
import com.jf3q.iot.service.SendInstructsService;
import com.jf3q.iot.utils.ElectricityMeterUtil;
import com.jf3q.iot.utils.InstructConstant;
import com.jf3q.iot.utils.InstructTypeConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class IotDeviceServiceImpl extends ServiceImpl<IotDeviceMapper, IotDevice>
    implements IotDeviceService{

    @Resource
    IotGatewayMapper gatewayMapper;

    @Resource
    SendInstructsService sendInstructsService;



    @Override
    public void operationElectricity(String operationType, Integer id) {


        //id设备id
        IotDevice device = baseMapper.selectById(id);
        if (device==null) {
            throw new RuntimeException("不存在该设备");
        }

        String deviceCode = device.getDeviceCode();
        Integer gatewayId = device.getGatewayId();
        IotGateway gatewayVO = gatewayMapper.selectById(gatewayId);
        if (gatewayVO==null) {
            throw new RuntimeException("该设备没有任何网关关联它");
        }



        //拿到处理后的设备编号
        String address = ElectricityMeterUtil.deviceAddressConvert(deviceCode);

        String ip = "/"+gatewayVO.getGatewayIp()+":"+gatewayVO.getGatewayPort();
        //获取发送指令  (是拉闸  还是合闸  还是…………)
        String instruce =getInstruce(address,operationType);

        if (instruce==null) {
            throw new RuntimeException("获取操作指令失败");
        }


        sendInstructsService.sendInstructs(instruce,ip,deviceCode,operationType);


    }

    @Override
    public void meterJob() {
        log.info("查出所有的电表设备的余额");
        //查出所有的电表设备的余额
        List<IotDevice> list = baseMapper.selectList(null);
        for (IotDevice device : list) {
            //查出网关
            IotGateway gatewayVO = gatewayMapper.selectById(device.getGatewayId());
            String address = ElectricityMeterUtil.deviceAddressConvert(device.getDeviceCode());
            String ip = "/"+gatewayVO.getGatewayIp()+":"+gatewayVO.getGatewayPort();
            String instruce = ElectricityMeterUtil.getCompleteInstruct(address, InstructConstant.ELECTRICITY_BALANCE);
            sendInstructsService.sendInstructs(instruce,ip,device.getDeviceCode(), InstructTypeConstant.E_BALANCE_SETUPA.getKey());
        }
    }

    private String getInstruce(String address,String operationType) {
        if (operationType.equals(InstructTypeConstant.E_MAIN_GATE_NO.getKey())) {//合总闸
            return ElectricityMeterUtil.getCompleteInstruct(address,
                    InstructConstant.ELECTRICITY_METER_ON_INSTRUCT);
        }else if(operationType.equals(InstructTypeConstant.E_MAIN_GATE_OFF.getKey())){//拉总闸
            return ElectricityMeterUtil.getCompleteInstruct(address,
                    InstructConstant.ELECTRICITY_METER_PULL_INSTRUCT);
        }else if(operationType.equals(InstructTypeConstant.E_A_GATE_NO.getKey())) {//电-A合总闸
            return ElectricityMeterUtil.getCompleteInstruct(address,
                    InstructConstant.ELECTRICITY_METER_A_NO_INSTRUCT);
        }else if(operationType.equals(InstructTypeConstant.E_A_GATE_OFF.getKey())) {//电-A合总闸
            return ElectricityMeterUtil.getCompleteInstruct(address,
                    InstructConstant.ELECTRICITY_METER_A_PULL_INSTRUCT);
        }else if(operationType.equals(InstructTypeConstant.E_B_GATE_NO.getKey())) {//电-A合总闸
            return ElectricityMeterUtil.getCompleteInstruct(address,
                    InstructConstant.ELECTRICITY_METER_B_NO_INSTRUCT);
        }else if(operationType.equals(InstructTypeConstant.E_B_GATE_OFF.getKey())) {//电-A合总闸
            return ElectricityMeterUtil.getCompleteInstruct(address,
                    InstructConstant.ELECTRICITY_METER_B_PULL_INSTRUCT);
        }

        return null;




    }
}




